package pcloud.org.server.identity;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.nutz.mvc.Mvcs;

public class AuthManager
{
	public static final String Attr_Client_Id = "client_id";
	public static final String Attr_User_Name = "user_name";

	public static User getUser(HttpSession session)
	{
		String userName = (String)session.getAttribute("user_name");
		if (userName==null) {
			throw new RuntimeException("No user info, please log in first!");
		}
		User user = User.getUser(userName);
		return user;
	}
	public static User getUser()
	{
		return getUser(Mvcs.getHttpSession());
	}
	public static Map<String, String>  login(String userName, String clientId)
	{
		if (userName==null) {
			return null;
		}
		HttpSession session = Mvcs.getHttpSession();
		session.setAttribute(Attr_User_Name, userName);
		session.setAttribute(Attr_Client_Id, clientId);
		
		Map<String, String> result = new HashMap<String, String> ();
		result.put(Attr_User_Name, (String)session.getAttribute(Attr_User_Name));
		result.put(Attr_Client_Id, (String)session.getAttribute(Attr_Client_Id));
		return result;		
	}
}
